package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        //根据三级分类查询分类品牌对应集合
        //select *from base_category_trademark where category3_id=61
        QueryWrapper <BaseCategoryTrademark> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);

        //根据对应关系集合，处理获取品牌id集合
        /**
         * stream中的map和filter区别：
         *
         * map:用户处理流中数据
         * filter:过滤
         *
         */
        //判断
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){


            //获取id列表
            List<Long> trademarkIds = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {


                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());


            ////根据品牌id列表查询品牌集合数据
//            List<BaseTrademark> baseTrademarkList = trademarkIds.stream().map(trademarkId -> {
//
//                BaseTrademark baseTrademark = baseTrademarkMapper.selectById(trademarkId);
//                return baseTrademark;
//
//            }).collect(Collectors.toList());

            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(trademarkIds);



            return baseTrademarkList;
        }


        return null;
    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        //根据三级分类查询分类品牌对应集合
        //select *from base_category_trademark where category3_id=61
        QueryWrapper <BaseCategoryTrademark> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);
        //处理获取已经关联的品牌id集合
        //判断
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> trademarkIds = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {

                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //查询搜索品牌
            List<BaseTrademark> currentTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {



                return !trademarkIds.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            return currentTrademarkList;

        }


        return baseTrademarkMapper.selectList(null);
    }

    /**
     * 保存分类品牌关联
     * @param categoryTrademarkVo
     */
    @Override
    public void saveCategoryTradeMark(CategoryTrademarkVo categoryTrademarkVo) {

        //获取选择的品牌id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        // 61 【4,5】
        // 61 4  BaseCategoryTradermark
        //61 5  BaseCategoryTradermark
        //判断
        if(!CollectionUtils.isEmpty(trademarkIdList)){

            List<BaseCategoryTrademark> categoryTrademarks = trademarkIdList.stream().map(trademarkId -> {
                //创建分类品牌对象
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);

                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            //保存
            this.saveBatch(categoryTrademarks);
        }




    }

    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void removeBaseCategoryTrademark(Long category3Id, Long trademarkId) {

        //select *from base_category_trademark where category3_id=? and trademark_id=?

        QueryWrapper<BaseCategoryTrademark> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",trademarkId);
        //删除
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }
}
